/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package admin.recordatorio;

import DBMS.DataBaseManager;
import admin.permisos.PermisosManager;
import clases.Recordatorio;
import clases.Usuario;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

/**
 *
 * @author jesus
 */
public class AgregarRecordatorio extends DispatchAction {

    /*
     * forward name="success" path=""
     */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    private static final String name = "Agregar Recordatorio";

    /**
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception 
     */
    public ActionForward save(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        Recordatorio recordatorio = (Recordatorio) form;

        if (recordatorio == null
                || recordatorio.getFechaInicio().isEmpty()
                || recordatorio.getFechaFin().isEmpty() || recordatorio.getNombre().isEmpty()) {
            recordatorio.setMensaje("No se ha podido agregar el recordatorio");
            return mapping.findForward(FAILURE);
        }


        DataBaseManager dbm = DataBaseManager.getInstance();
        ArrayList<Recordatorio> valores;
        if (dbm.agregarRecordatorio(recordatorio)) {
            recordatorio.setMensajeOK("Recordatorio agregado satisfactoriamente.");
            request.setAttribute("recordatorio", recordatorio);
            valores = dbm.consultarRecordatorios(new Recordatorio());
            request.setAttribute("recordatorios", valores);
            return mapping.findForward(SUCCESS);
        } else {
            recordatorio.setMensaje("No ha podido agregarse el recordatorio a "
                    + "la base de datos.");
            request.setAttribute("recordatorio", recordatorio);
            valores = dbm.consultarRecordatorios(new Recordatorio());
            request.setAttribute("recordatorios", valores);
            return mapping.findForward(FAILURE);
        }
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
//        Usuario usr = (Usuario) request.getSession().getAttribute("logueado");
//        DataBaseManager dbms = DataBaseManager.getInstance();
//        if (!PermisosManager.validar(this.name, dbms.consultarPermisoAccion(name), usr.getPermiso())) {
//            System.out.println("No tiene permiso");
//            return mapping.findForward(FAILURE);
//        }
        DataBaseManager dbms = DataBaseManager.getInstance();
        ArrayList<String> fechas = DataBaseManager.getFechas();
        System.out.println("Tamano de fechas"+ fechas.size());
        request.setAttribute("anos", fechas);
        return mapping.findForward("page");
    }
}
